package com.udaos.modules.customer.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.adaptor.WhaleAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.annotation.SLog;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.customer.Ud_customer;
import com.udaos.modules.customer.services.UdCustomerService;


/**
 * 会员
 * @author mutaotao
 * @time 2016-11-08 14:42:56
 * 
 */
@IocBean
@At("/platform/customer")
@Filters({ @By(type = PrivateFilter.class)})
public class UdCustomerController {
	@Inject
	private UdCustomerService udCustomerService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/customer/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param mobile
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full") 
	@RequiresAuthentication
	public Object data( @Param("name") String name,
			@Param("mobile") String mobile, @Param("length") int length, @Param("start") int start,
			@Param("draw") int draw, @Param("::order") List<DataTableOrder> order,
			@Param("::columns") List<DataTableColumn> columns) {
			
			Cnd cnd = Cnd.NEW();
			cnd.and("delFlag", "=", "0");
			if (!Strings.isBlank(mobile))
				cnd.and("mobile", "like", "%" + mobile + "%");
			if (!Strings.isBlank(name))
				cnd.and("name", "like", "%" + name + "%");
			return udCustomerService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增用户
	 */
	@At
	@Ok("beetl:/platform/customer/add.html")
	@RequiresAuthentication
	public void add() {
	
	}
	
	/**
	 * 新增用戶
	 * @param customer
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	@SLog(tag = "新建用户", msg = "用户名:${args[0].name}")
	public Object addDo(@Param("..") Ud_customer customer, HttpServletRequest req) {
		try {
			udCustomerService.insert(customer);
	        return Result.success("system.success");
	    } catch (Exception e) {
	    	return Result.error("system.error");
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/customer/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		return udCustomerService.fetch(id);
	}
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	@SLog(tag = "修改用户", msg = "用户名:${args[0].name}")
	public Object editDo(@Param("..")Ud_customer customer, HttpServletRequest req) {
		try {
			customer.setOpAt((int)(System.currentTimeMillis()/1000));
			udCustomerService.updateIgnoreNull(customer);
	   		return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    @SLog(tag = "删除用户", msg = "用户名:${args[1].getAttribute('name')}")
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_customer customer = udCustomerService.fetch(id);
        	customer.setDelFlag(true);
        	udCustomerService.updateIgnoreNull(customer);
            req.setAttribute("name", customer.getName());
            return Result.success("system.success");
        } catch (Exception e) {
            return Result.error("system.error");
        }
    }
	
	
	

}